<template>
  <div class="faq-container">
    <!-- 顶部导航栏 -->
    <div class="faq-header">
      <div class="back-button" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </div>
      <div class="header-title">常见问题</div>
      <div class="placeholder"></div>
    </div>

    <!-- 内容区域 -->
    <div class="faq-content">
      <!-- 分类筛选 -->
      <div class="category-filter">
        <el-tabs v-model="activeCategory" @tab-change="handleCategoryChange">
          <el-tab-pane label="全部" name="all" />
          <el-tab-pane 
            v-for="category in categories" 
            :key="category.value"
            :label="category.label" 
            :name="category.value.toString()" 
          />
        </el-tabs>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>

      <!-- FAQ列表 -->
      <div v-else-if="faqList.length > 0" class="faq-list">
        <el-collapse v-model="activeNames" accordion>
          <el-collapse-item 
            v-for="faq in faqList" 
            :key="faq.id" 
            :name="faq.id.toString()"
            class="faq-item"
          >
            <template #title>
              <div class="faq-title">
                <el-tag 
                  :type="getCategoryTagType(faq.category)" 
                  size="small"
                  class="category-tag"
                >
                  {{ faq.categoryName }}
                </el-tag>
                <span class="title-text">{{ faq.title }}</span>
              </div>
            </template>
            
            <div class="faq-content-text" v-html="formatContent(faq.content)"></div>
          </el-collapse-item>
        </el-collapse>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-container">
        <el-empty description="暂无相关问题">
          <el-button type="primary" @click="refreshFAQs">
            刷新
          </el-button>
        </el-empty>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { ArrowLeft } from '@element-plus/icons-vue';
import { useFAQStore } from '@/store';
import faqService from '@/services/faqService';

const router = useRouter();
const faqStore = useFAQStore();

// 响应式数据
const faqList = ref([]);
const activeCategory = ref('all');
const activeNames = ref([]);
const categories = ref([]);

// 计算属性
const loading = computed(() => faqStore.isLoading);

// 返回上一页
const goBack = () => {
  router.back();
};

// 获取分类标签类型
const getCategoryTagType = (category) => {
  const typeMap = {
    0: 'primary',  // 贷款
    1: 'success',  // 还款
    2: 'warning',  // 账户
    3: 'info'      // 其他
  };
  return typeMap[category] || 'info';
};

// 格式化内容
const formatContent = (content) => {
  if (!content) return '';
  // 将换行符转换为HTML换行
  return content.replace(/\n/g, '<br>');
};

// 获取FAQ列表
const fetchFAQs = async (category = null) => {
  try {
    let faqs;
    
    if (category === null || category === 'all') {
      faqs = await faqService.getAllFAQs();
    } else {
      faqs = await faqService.getFAQsByCategory(parseInt(category));
    }
    console.log(faqs,category)
    faqList.value = faqs;
  } catch (error) {
    ElMessage.error('获取FAQ列表失败');
  }
};

// 处理分类切换
const handleCategoryChange = (category) => {
  activeNames.value = []; // 关闭所有展开的项
  fetchFAQs(category);
};

// 刷新FAQ列表
const refreshFAQs = () => {
  fetchFAQs(activeCategory.value === 'all' ? null : activeCategory.value);
};

// 页面加载时获取数据
onMounted(async () => {
  // 获取分类列表
  categories.value = faqService.getAllCategories();
  
  // 获取FAQ列表
  await fetchFAQs();
});
</script>

<style scoped lang="scss">
.faq-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f7fa;
}

.faq-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  background-color: #3a4a7f;
  color: white;
  padding: 0 15px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-button {
  font-size: 20px;
  cursor: pointer;
  padding: 5px;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
}

.placeholder {
  width: 20px;
}

.faq-content {
  flex: 1;
  overflow-y: auto;
}

.category-filter {
  background: white;
  padding: 0 15px;
  border-bottom: 1px solid #eee;
  
  :deep(.el-tabs__header) {
    margin: 0;
  }
  
  :deep(.el-tabs__nav-wrap) {
    padding: 0;
  }
  
  :deep(.el-tabs__item) {
    padding: 0 20px;
    height: 44px;
    line-height: 44px;
  }
}

.loading-container {
  padding: 20px;
}

.faq-list {
  padding: 15px;
}

.faq-item {
  margin-bottom: 10px;
  border-radius: 8px;
  overflow: hidden;
  
  :deep(.el-collapse-item__header) {
    background-color: white;
    border: none;
    padding: 16px;
    font-weight: 500;
  }
  
  :deep(.el-collapse-item__content) {
    background-color: #fafafa;
    border: none;
    padding: 16px;
  }
}

.faq-title {
  display: flex;
  align-items: center;
  width: 100%;
  
  .category-tag {
    margin-right: 8px;
    flex-shrink: 0;
  }
  
  .title-text {
    flex: 1;
    text-align: left;
    font-size: 14px;
    line-height: 1.4;
  }
}

.faq-content-text {
  font-size: 14px;
  line-height: 1.6;
  color: #666;
  
  :deep(br) {
    margin-bottom: 8px;
  }
}

.empty-container {
  padding: 60px 20px;
  text-align: center;
}

// 响应式设计
@media (max-width: 768px) {
  .category-filter {
    padding: 0 10px;
    
    :deep(.el-tabs__item) {
      padding: 0 15px;
      font-size: 14px;
    }
  }
  
  .faq-list {
    padding: 10px;
  }
  
  .faq-item {
    :deep(.el-collapse-item__header) {
      padding: 12px;
    }
    
    :deep(.el-collapse-item__content) {
      padding: 12px;
    }
  }
  
  .faq-title .title-text {
    font-size: 13px;
  }
  
  .faq-content-text {
    font-size: 13px;
  }
}
</style>